####################################################################################
## xu xian feng
## 2022/03/30
## LUAD analysis
####################################################################################

source ~/20220915_gastric_multiple/dna_combine/config/config.sh
export config_file=~/20220915_gastric_multiple/dna_combine/config/config.sh

####################################################################################
## 得到上游处理好的文件
## 去除存在污染的样本
## vcf、maf、seg、mosdepth、msi
sh ${scripts_path}/module/get_genomic_file.sh

## 合并所有质控文件
${Rscript} ${scripts_path}/combineQC.R \
--msi_file ${Qc_path}/All_Msi.tsv \
--depth_file ${Qc_path}/Depth_summary.txt \
--info_file ${config_path}/tumor_normal.class.list \
--mutNum_file ${Qc_path}/Vcf_QC.list \
--out_file ${Qc_path}/Summary_Qc.tsv

## 合并纯度
${Rscript} ${scripts_path}/combinePurity.R \
--purity_file ${Titan_path}/Purity_titan.final.tsv \
--qc_file ${Qc_path}/Summary_Qc.tsv \
--info_file ${config_path}/tumor_normal.class.list \
--out_file ${Qc_path}/Summary_Qc.addPurity.tsv

####################################################################################
## 质控MSI的样本
## https://github.com/vanallenlab/MSIsensor/blob/master/README.md
## https://www.advancesinmolecularpathology.com/article/S2589-4080(21)00016-8/pdf
## Tumor和Normal配对的时候，MSI>3.5认为MSI-High
## 去除MSI-HIGH的样本
## 12个人的胃癌样本
## 11个人MSI>10
msi_high_sample=`cat ${Qc_path}/Summary_Qc.tsv | grep -v Tumor | awk -F'\t' '{if($8 > 10)print $2}' | sort -u |\
tr '\n' '|' | sed 's/|$//'`
## 1个人突变数量极高3680
## 有POLL、POLE、POLR2A、POLD1的CDS突变,MSI为2.93
## 也认为是超突变
msi_high_sample=`echo "${msi_high_sample}|JRGC00009"`

## 以下三个Tumor的样本,突变数量不到100个，纯度低于0.1
lowMutTumorSample="S37|JZGC00762|JZGC00750"
## S37为DGC，去除以后剩下的为IM和DGC
cat ${config_path}/tumor_normal.class.bk.list | grep -v -E -w ${msi_high_sample} | grep -v -E -w ${lowMutTumorSample} |\
awk -F'\t' '{OFS="\t"}{if($1=="JZGC00580"){$6="IM + IGC"}print}' \
> ${config_path}/tumor_normal.class.list

## JZGC00580的类别进行替换
## IM+IGC+DGC替换为IM+IGC
cp ${Qc_path}/Summary_Qc.tsv ${Qc_path}/Summary_Qc.raw.tsv
cp ${Qc_path}/Summary_Qc.addPurity.tsv ${Qc_path}/Summary_Qc.addPurity.raw.tsv

cat ${Qc_path}/Summary_Qc.raw.tsv | awk -F'\t' '{OFS="\t"}{if($2=="JZGC00580"){$6="IM + IGC"}print}' \
> ${Qc_path}/Summary_Qc.tsv
cat ${Qc_path}/Summary_Qc.addPurity.raw.tsv | awk -F'\t' '{OFS="\t"}{if($2=="JZGC00580"){$6="IM + IGC"}print}' \
> ${Qc_path}/Summary_Qc.addPurity.tsv

####################################################################################
## 不考虑class，最标准的tumor+normal的格式
cat ${config_path}/tumor_normal.class.list | awk -F'\t' '{OFS=","}{print $3,$2}' \
> ${config_path}/tumor_normal.list

## IM的样本
im_sample=`cat ${config_path}/tumor_normal.class.list  | awk -F'\t' '{OFS="\t"}{if($4=="IM")print $3 }' | \
tr '\n' '|' | sed 's/|$//'`

## GC的样本
gc_sample=`cat ${config_path}/tumor_normal.class.list  | awk -F'\t' '{OFS="\t"}{if($4~"GC")print $3 }' | \
tr '\n' '|' | sed 's/|$//'`

cat ${config_path}/tumor_normal.list > ${config_path}/tumor_normal.all.list
cat ${config_path}/tumor_normal.list | grep -E -w "${im_sample}|Tumor" > ${config_path}/tumor_normal.precancer.list
cat ${config_path}/tumor_normal.list | grep -E -w "${gc_sample}|Tumor" > ${config_path}/tumor_normal.cancer.list

####################################################################################
#### 整理基线
## 整理年龄、性别、吸烟、饮酒等信息
## 注释整体和CDS的覆盖区域、倍体、纯度、质控信息
${Rscript_mutationTime} ${scripts_path}/CompareBaseLine.R \
--tumor_list ${config_path}/tumor_normal.class.list \
--baseline_file ${config_path}/STAD_MutipleReigon_baseline.tsv \
--qc_file ${Qc_path}/Summary_Qc.tsv \
--burden_all_file ${Qc_path}/Burden.coverage10x.Autosomal.txt \
--burden_cds_file ${Qc_path}/Burden.coverage10x.Autosomal.cds.txt \
--purity_file ${Titan_path}/Purity_titan.final.tsv \
--out_dir ${config_path}

####################################################################################
#### 提取RNA和DNA均有的样本
sh ${scripts_path}/module/getRNAData.sh


####################################################################################
#### MutSig2CV 编码区
## 总体、癌前和癌分开算
for class in ${class_type[@]}
do
echo $class
sh ${scripts_path}/module/mutsig2cv_coding.class.sh ${class}
done

####################################################################################
#### gistic2
## 总体、癌前和癌分开算
## 还未跑通
for class in ${class_type[@]}
do
echo $class
sh ${scripts_path}/module/gistic2.class.sh ${class}
done

####################################################################################
#### CHAT
#### 计算CCF
sh ${scripts_path}/module/computeCCF.sh

####################################################################################
#### pyclone
#### 计算聚类
sh ${scripts_path}/module/computePyclone.SingleSample.sh

####################################################################################
#### Mutationtime
#### 计算突变时间
sh ${scripts_path}/module/computeMutaionTime.sh

####################################################################################
## 分病理类型合并GGA以后的MAF
for class in ${class_type[@]}
do
echo $class
## 头行注释
cat ${maf_path}/*_GGA_Filter_funcotated.maf | grep -v "#" | head -1 > ${maf_path}/All_GGA.${class}.maf
for line in `cat ${config_path}/tumor_normal.${class}.list | grep -v Tumor`
do
Tumor=`echo ${line} | awk -F, '{print $1}'`
Normal=`echo ${line} | awk -F, '{print $2}'`
cat ${maf_path}/${Tumor}_${Normal}_GGA_Filter_funcotated.maf | grep -v -E "##|Hugo_Symbol|#version"  | \
awk -F'\t' '{OFS="\t"}{$16=Tumor;print}' Tumor=${Tumor} \
>> ${maf_path}/All_GGA.${class}.maf
done
done

####################################################################################
##################################### 突变负荷计算
## 使用QC的maf文件
sh ${scripts_path}/mutBurden/mutBurden_getmaf.sh

## IM、IGC和DGC的突变负荷的比较
${Rscript} ${scripts_path}/mutBurden/mutBurden_plot.R \
--info_file ${config_path}/STAD-useCombine.Sample.tsv \
--maf_file ${maf_path}/All_ForMutBurden.extract.maf \
--images_path ${Images_path}/mutBurden

##################################### 突变负荷和基线的比较
ln -snf ${Images_path}/mutBurden/mutBurden.tsv ${baseTable_path}/STAD_Info.addBurden.tsv

## 突变负荷与年龄的关系
## 年龄为连续性变量
${Rscript} ${scripts_path}/mutBurden/mutBurden_plot.Age.R \
--input_file ${baseTable_path}/STAD_Info.addBurden.tsv \
--images_path ${Images_path}/mutBurden

## 突变负荷与性别、吸烟、饮酒、HP的关系
${Rscript} ${scripts_path}/mutBurden/mutBurden_plot.Baseline.R \
--input_file ${baseTable_path}/STAD_Info.addBurden.tsv \
--images_path ${Images_path}/mutBurden

####################################################################################
##################################### 基因突变率的计算和recurrent位点的计算
## 所有基因突变率的计算，IM、IGC、DGC、GC
${Rscript} ${scripts_path}/plot/mutRate_compute.R \
--maf_file ${maf_path}/All_GGA.all.maf \
--images_path ${Images_path}/mutRate \
--info_file ${config_path}/tumor_normal.class.list

## 所有多次出现突变位点的突变率的计算，IM、IGC、DGC、GC
${Rscript} ${scripts_path}/plot/mutRate_compute.RecurrentPoint.R \
--maf_file ${maf_path}/All_GGA.all.maf \
--images_path ${Images_path}/mutRate \
--info_file ${config_path}/tumor_normal.class.list

## 基因突变的共享情况
## 以人为单位，在几个人中为癌前和癌共享的突变，在几个人中为癌前和癌私有的突变(功能性突变)
${Rscript} ${scripts_path}/plot/mutShare_compute.R \
--maf_file ${maf_path}/All_GGA.all.maf \
--images_path ${Images_path}/mutRate \
--info_file ${config_path}/tumor_normal.class.list

## 多次出现突变位点的共享情况
${Rscript} ${scripts_path}/plot/mutShare_compute.RecurrentPoint.R \
--maf_file ${maf_path}/All_GGA.all.maf \
--recurrent_point_file ${Images_path}/mutRate/MutRate.RecurrentPoint.tsv \
--images_path ${Images_path}/mutRate \
--info_file ${config_path}/tumor_normal.class.list

## 所有突变位点计算是否共享
## 用于突变信号分类
${Rscript} ${scripts_path}/plot/mutShare_compute.AllPoint.R \
--maf_file ${maf_path}/All_GGA.all.maf \
--images_path ${Images_path}/mutRate \
--info_file ${config_path}/tumor_normal.class.list


##################################### 驱动基因的检查
## 计算突变率
for class in ${class_type[@]}
do
echo $class
${Rscript} ${scripts_path}/mutsig/Mutsig_check.R \
--class ${class} \
--sig_file ${MutsigOut_path}/${class}/sig_genes.txt \
--smg_file ${work_dir}/public_ref/SMG_sort.list \
--mutRate_file ${Images_path}/mutRate/MutRate.tsv \
--out_path ${mutsig_check_path}/${class} 
done

## 每个基因检查突变是否可靠
for class in ${class_type[@]}
do
echo $class
for gene in `cat ${mutsig_check_path}/${class}/mutRate.${class}.tsv | sed '1d' | awk -F"\t" '{print $1}'`
do
${Rscript} ${scripts_path}/plot/Lollipop_variant.R \
--gene ${gene} \
--pre_file ${maf_path}/All_GGA.precancer.maf \
--cancer_file ${maf_path}/All_GGA.cancer.maf \
--sample_info ${config_path}/tumor_normal.class.list \
--gtf_file ${ref_path}/GTF/gencode.v19.annotation.exonNum.gtf \
--out_path ${mutsig_check_path}/${class}/${gene}
done
done

## 可靠的基因列表
# ${mutsig_check_path}/cancer/cancer.smg.list
# ${mutsig_check_path}/precancer/precancer.smg.list
## 合成一个文件
echo "Gene_Symbol" > ${mutsig_check_path}/smg.list
cat ${mutsig_check_path}/cancer/cancer.smg.list ${mutsig_check_path}/precancer/precancer.smg.list | sort -u \
>> ${mutsig_check_path}/smg.list

## venn图展示共享和私有情况



#####################################
## 驱动基因的列表整理
## 整理q值和突变率
## ${mutsig_check_path}/driver.summary.tsv
${Rscript} ${scripts_path}/mutsig/Mutsig_combineInfo.R \
--sig_cancer_file ${MutsigOut_path}/cancer/sig_genes.txt \
--sig_pre_file ${MutsigOut_path}/precancer/sig_genes.txt \
--smg_file ${mutsig_check_path}/smg.list \
--reprot_file ${work_dir}/public_ref/SMG_sort.list \
--mutRate_file ${Images_path}/mutRate/MutRate.tsv \
--out_path ${mutsig_check_path}

## 驱动基因突变率在IM和GC、IGC和DGC的计算
${Rscript} ${scripts_path}/plot/mutRate_plot.All.R \
--smg_file ${mutsig_check_path}/smg.list \
--mut_rate_gene_file ${Images_path}/mutRate/MutRate.tsv \
--images_path ${Images_path}/mutRatePlot


##################################### 突变瀑布图
## 展示鉴定的驱动基因的突变率
## 左边*为Tumor鉴定的，右边*为IM鉴定的
${Rscript} ${scripts_path}/plot/waterfull_smg.R \
--maf_path ${maf_path} \
--images_path ${Images_path} \
--info_file ${config_path}/tumor_normal.class.list \
--class_order_file ${config_path}/Class_order.list \
--cancer_list ${mutsig_check_path}/cancer/cancer.smg.list \
--precancer_list ${mutsig_check_path}/precancer/precancer.smg.list

## 驱动基因按人展示
${Rscript} ${scripts_path}/plot/waterfull_smg.SortBySample.R \
--maf_path ${maf_path} \
--images_path ${Images_path} \
--info_file ${config_path}/tumor_normal.class.list \
--class_order_file ${config_path}/Class_order.list \
--class_order_sub_file ${config_path}/Class_order_sub.list \
--cancer_list ${mutsig_check_path}/cancer/cancer.smg.list \
--precancer_list ${mutsig_check_path}/precancer/precancer.smg.list

## 基因的互斥


## 所有已报道胃癌驱动基因突变情况
## 出现在至少5个样本
${Rscript} ${scripts_path}/plot/waterfull_smg.reportSMG.R \
--maf_path ${maf_path} \
--images_path ${Images_path} \
--info_file ${config_path}/tumor_normal.class.list \
--class_order_file ${config_path}/Class_order.list \
--smg_list ${work_dir}/public_ref/SMG_sort.list 

##################################### 提取关键基因的突变特征
for gene in `cat ${mutsig_check_path}/smg.list | grep -v Gene_Symbol | sort -u`
do
#### 突变棒棒糖图
${Rscript} ${scripts_path}/plot/Lollipop_variant.R \
--gene ${gene} \
--pre_file ${maf_path}/All_GGA.precancer.maf \
--cancer_file ${maf_path}/All_GGA.cancer.maf \
--sample_info ${config_path}/tumor_normal.class.list \
--gtf_file ${ref_path}/GTF/gencode.v19.annotation.exonNum.gtf \
--out_path ${Images_path}/lollipop/${gene}

#### IM和GC的突变率的比较 
## 整体基因的和Recurrent突变位点的
${Rscript} ${scripts_path}/plot/mutRate_plot.R \
--gene ${gene} \
--mut_rate_gene_file ${Images_path}/mutRate/MutRate.tsv \
--mut_rate_point_file ${Images_path}/mutRate/MutRate.RecurrentPoint.tsv \
--images_path ${Images_path}/mutRatePlot

#### IM和IGC和DGC的突变率的比较 
## 整体基因的和Recurrent突变位点的
${Rscript} ${scripts_path}/plot/mutRate_plot.IM_IGC_DGC.R \
--gene ${gene} \
--mut_rate_gene_file ${Images_path}/mutRate/MutRate.tsv \
--mut_rate_point_file ${Images_path}/mutRate/MutRate.RecurrentPoint.tsv \
--images_path ${Images_path}/mutRatePlot

#### 在癌前和癌的共享情况
${Rscript} ${scripts_path}/plot/mutShare_plot.R \
--gene ${gene} \
--mut_share_file ${Images_path}/mutRate/MutShare.tsv \
--mut_share_point_file ${Images_path}/mutRate/MutShare.RecurrentPoint.tsv \
--images_path ${Images_path}/mutRatePlot

#### 基因的CCF在样本间的变化情况
## 盒图
${Rscript} ${scripts_path}/plot/mutCCF_plot.R \
--gene ${gene} \
--info_file ${config_path}/STAD-useCombine.Sample.tsv \
--ccf_file ${MutationTime_path}/result/All_CCF_mutTime.tsv \
--images_path ${Images_path}/mutCCF

## 柱状图
${Rscript} ${scripts_path}/plot/mutCCF_plot.bar.R \
--gene ${gene} \
--info_file ${config_path}/STAD-useCombine.Sample.tsv \
--class_sub_file ${config_path}/Class_order_sub.list \
--lollipop_file ${Images_path}/lollipop/${gene}/${gene}.AllInfo.tsv \
--ccf_file ${MutationTime_path}/result/All_CCF_mutTime.tsv \
--images_path ${Images_path}/mutCCF

#### 关键驱动基因突变与性别、吸烟、饮酒、HP的关系
${Rscript} ${scripts_path}/plot/mutBaseline_plot.R \
--gene ${gene} \
--info_file ${config_path}/STAD-useCombine.Sample.tsv \
--ccf_file ${MutationTime_path}/result/All_CCF_mutTime.tsv \
--images_path ${Images_path}/mutBaselinePlot/${gene}

#### 基因的表达
## 整体的表达情况
${Rscript} ${scripts_path}/mRNA/showGene.Normalize.R \
--sample_list_file ${config_path}/tumor_normal.class.list \
--rsem_file ${mRNA_path}/CombineTMM.DNAUse.MergeMutiSample.tsv \
--out_path ${Images_path}/expression \
--gtf_file ${ref_path}/GTF/gencode.v19.ensg_genename.txt \
--gene ${gene}

## Normal、IM、IGC、DGC
${Rscript} ${scripts_path}/mRNA/showGene.Normalize.oneImage.R \
--sample_list_file ${config_path}/tumor_normal.class.list \
--rsem_file ${mRNA_path}/CombineTMM.DNAUse.MergeMutiSample.tsv \
--out_path ${Images_path}/expression \
--gtf_file ${ref_path}/GTF/gencode.v19.ensg_genename.txt \
--gene ${gene}

## 突变和非突变的表达
${Rscript} ${scripts_path}/mRNA/showGene.Normalize.MutvsWild.R \
--sample_list_file ${config_path}/tumor_normal.class.list \
--rsem_file ${mRNA_path}/CombineCounts.FilterLowExpression.TMM.tsv \
--out_path ${Images_path}/expression \
--lollipop_file ${Images_path}/lollipop/${gene}/${gene}.AllInfo.tsv \
--gtf_file ${ref_path}/GTF/gencode.v19.ensg_genename.txt \
--gene ${gene}

done

####################################################################################
## TP53的专门比较
## TP53突变率在IGC和DGC的差异
${Rscript} ${scripts_path}/driverGene/mutRate_plot.TP53.R \
--gene TP53 \
--mut_rate_gene_file ${Images_path}/mutRate/MutRate.tsv \
--mut_rate_point_file ${Images_path}/mutRate/MutRate.RecurrentPoint.tsv \
--images_path ${Images_path}/mutRatePlot

## TP53突变样本，其突变负荷的差异
${Rscript} ${scripts_path}/driverGene/mutBurden_plot.TP53.R \
--input_file ${baseTable_path}/STAD_Info.addBurden.tsv \
--ccf_file ${MutationTime_path}/result/All_CCF_mutTime.tsv \
--images_path ${Images_path}/mutBurden

## TP53出现一次的突变和多次出现的突变样本，其突变负荷的差异
${Rscript} ${scripts_path}/driverGene/mutBurden_plot.RecurrentPoint.TP53.R \
--input_file ${baseTable_path}/STAD_Info.addBurden.tsv \
--ccf_file ${MutationTime_path}/result/All_CCF_mutTime.tsv \
--images_path ${Images_path}/mutBurden

## Trunk突变和Private突变突变负荷的比较
${Rscript} ${scripts_path}/driverGene/mutBurden_plot.ShareMut.TP53.R \
--input_file ${baseTable_path}/STAD_Info.addBurden.tsv \
--ccf_file ${MutationTime_path}/result/All_CCF_mutTime.tsv \
--images_path ${Images_path}/mutBurden


####################################################################################
## 公共数据的比较
## tcga和oncosg
## 突变负荷、TP53突变率
sh ${scripts_path}/module/compare_publicData.sh


####################################################################################
##################################### 突变信号计算
## 1、基于GGA，分为总体突变信号、trunk突变信号和private突变信号
## 分别产生对应的vcf文件
rm -rf ${SigProfiler_path}/vcf/

cat ${config_path}/tumor_normal.list | tr ',' ' ' | grep -v Normal | xargs -P 10 -i sh -c '
echo {}
sh ${scripts_path}/sigprofile/denovoSig_1_getPassVcf.sh {}
'

## 2、提取突变信号
## 进入python中可以运行，直接运行脚本会有Bug
${python} ${scripts_path}/sigprofile/denovoSig_2_SigProfiler.all.py
${python} ${scripts_path}/sigprofile/denovoSig_2_SigProfiler.trunk.py
${python} ${scripts_path}/sigprofile/denovoSig_2_SigProfiler.private.py

## 3、denovoToDecompose
share_list=( "all" "trunk" "private" )
for share in ${share_list[@]}
do
${python} ${scripts_path}/sigprofile/denovoSig_3_decompose.py \
${SigProfiler_path}/extractor/${share}
done

## 4、decompose的文件链接
for share in ${share_list[@]}
do
echo ${share}
## decompose的信号组成
cp -rf ${SigProfiler_path}/extractor/${share}/decompose/SBS96/Decompose_Solution/Activities/Decompose_Solution_Activities.txt \
${SigProfiler_path}/decompose/${share}_SBS96.txt
## 总体的突变数量
cp -rf ${SigProfiler_path}/extractor/${share}/SBS96/Samples.txt ${SigProfiler_path}/decompose/${share}_SBS96.AllMuts.txt
done

## 5、突变信号分布画图
## 总的
${Rscript} ${scripts_path}/sigprofile/Sigprofiler_decompose_plot_v2.R \
--work_dir ${SigProfiler_path}/decompose/ \
--base_line_file ${config_path}/tumor_normal.class.list \
--images_path ${SigProfiler_path}/plot
## 每个人的画突变信号的分布，看突变信号是在总的样本中如此还是个别样本的情况
${Rscript} ${scripts_path}/sigprofile/Sigprofiler_decompose_plot_v2.everysample.R \
--work_dir ${SigProfiler_path}/decompose/ \
--base_line_file ${config_path}/tumor_normal.class.list \
--images_path ${SigProfiler_path}/plot


####################################################################################
## 拷贝数的比较
## CNV的负荷
${Rscript} ${scripts_path}/plot/cnvBurden_plot.R \
--sample_file ${config_path}/tumor_normal.class.list \
--seg_file ${Titan_path}/Titan_all_seg.final.tsv \
--class_order_file ${config_path}/Class_order.list \
--images_path ${Images_path}/cnv_burden

## CNV的染色体分布
${Rscript} ${scripts_path}/plot/cnvDistribution_plot.R \
--sample_file ${config_path}/tumor_normal.class.list \
--seg_file ${Titan_path}/Titan_all_seg.final.tsv \
--class_order_file ${config_path}/Class_order.list \
--images_path ${Images_path}/cnv_burden

####################################################################################
## 肿瘤异质性的计算
## 1、比较共享驱动基因和不共享的两组瘤内异质性的差异
## 2、IM_IM|GC和GC_GC的瘤内异质性的差异
## 3、比较IM_IM,IM_IGC,IM_DGC的瘤内异质性
${Rscript} ${scripts_path}/tree/ComputeHeterogeneity.R \
--muti_cancer ${maf_path}/All_GGA.cancer.maf \
--muti_pre ${maf_path}/All_GGA.precancer.maf \
--gene_list ${work_dir}/public_ref/importTantGene.list \
--sample_info ${config_path}/tumor_normal.class.list \
--out_path ${Images_path}/ITH

## 计算驱动基因在多少样本中，IM和GC是共享的，IM和GC是各自独有的
${Rscript} ${scripts_path}/tree/JudgeGeneDriverSubtype.R \
--muti_cancer ${maf_path}/All_GGA.cancer.maf \
--muti_pre ${maf_path}/All_GGA.precancer.maf \
--gene_list ${work_dir}/public_ref/importTantGene.list \
--sample_info ${config_path}/tumor_normal.class.list \
--out_path ${Images_path}/ITH

####################################################################################
## 构建系统发生树
##################################### 计算每个人样本的数量
## 2个人构建进化树纳入所有突变不考虑vaf
## 超过3个人构建进化树需考虑vaf使树更可靠
${Rscript} ${scripts_path}/tree/computSampleNum.R \
--info_file ${config_path}/tumor_normal.class.list \
--out_file ${tree_path}/tumor_normal.class.forTree.list

## 描述每个样本的driver基因VAF情况以及对应样本的突变
## 为后期检查进化树
${Rscript} ${scripts_path}/tree/GetDriverEverySample.R \
--ccf_file ${MutationTime_path}/result/All_CCF_mutTime.tsv \
--gene_list ${work_dir}/public_ref/importTantGene.list \
--sample_info ${config_path}/tumor_normal.class.list \
--out_path ${Images_path}/DriverEverySample \
--class_order_file ${config_path}/Class_order_sub.list 

##################################### 环境配置
## 系统发生树
## 环境在950上
## 拷过去操作
export PROOT_NO_SECCOMP=1
HOME=/public/user/xxf2019

${HOME}/udocker run -v /public:/public treeomics bash

##################################### 以下操作在udocker里面进行
Xvfb -ac :11 -screen 0 1280x1024x8 &
export  DISPLAY=:11

## 环境配置
export HOME=/public/user/xxf2019
export work_dir=~/20220915_gastric_multiple/dna_combine
source ${work_dir}/config/config.sh

##################################### 连接需要展示的基因集

echo "Gene_Symbol" > ${work_dir}/public_ref/importTantGene.list
cat ${work_dir}/mutsig_check/smg.list ${work_dir}/public_ref/SMG_sort.list | grep -v Gene_Symbol |\
sort -u >> ${work_dir}/public_ref/importTantGene.list

mv /treeomics/input/SMG_sort.txt /treeomics/input/SMG_sort.bk.txt
ln -snf ${work_dir}/public_ref/importTantGene.list /treeomics/input/SMG_sort.txt

##################################### 跑进化树
HOME=/
## conda install -c bioconda pyensembl
## pyensembl install --release 75 --species homo_sapiens
## 使用纳入所有突变的配置文件
## 当样本数量小于3个时
## 构建进化树，纳入所有突变
cp -f /root/treeomics/treeomics/patient.py.revise /root/treeomics/treeomics/patient.py

export min_vaf=0.0001
export error=0.00000001
cat ${tree_path}/tumor_normal.class.forTree.list | grep -v Normal | awk -F'\t' '{if($8<=2)print $2}' | sort -u | xargs -P 20 -I Normal sh -c '
echo Normal
sh ${scripts_path}/tree/Treeomics_Tree.sh Normal ${config_path} ${min_vaf} ${error}
'

## 当含有大于2个样本的时候，纳入所有突变会造成进化树构建不准确
cp -f /root/treeomics/treeomics/patient.py.raw /root/treeomics/treeomics/patient.py 
export min_vaf=0.0001
export error=0.00000001
cat ${tree_path}/tumor_normal.class.forTree.list | grep -v Normal | awk -F'\t' '{if($8>2)print $2}' | sort -u  | xargs -P 20 -I Normal sh -c '
echo Normal
sh ${scripts_path}/tree/Treeomics_Tree.sh Normal ${config_path} ${min_vaf} ${error}
'

##################################### 拷贝文件
rm -rf ${tree_path}/Tree_file
mkdir ${tree_path}/Tree_file

cat ${tree_path}/tumor_normal.class.forTree.list | grep -v Normal | awk -F'\t' '{print $2}' | \
sort -u | xargs -P 10 -I Normal sh -c '
echo Normal
sh ${scripts_path}/tree/Treeomics_moveTree.sh Normal ${config_path}
'

##################################### 进行分类
mkdir -p ${tree_path}/TreeClass/NoTrunkDriver
mkdir -p ${tree_path}/TreeClass/TrunkDriver

for id in `cat ${Images_path}/ITH/DriverClass.tsv | grep -v Normal | awk '{print $1}'`
do
echo $id
normal=`cat ${config_path}/tumor_normal.class.list | grep ${id} | awk '{print $2}' | sort -u`
tree_class=`cat ${Images_path}/ITH/DriverClass.tsv | grep -w ${id} | awk -F'\t' '{print $3}'`

ln -snf ${tree_path}/Tree_file/${normal}_mlhtree.pdf ${tree_path}/TreeClass/${tree_class}/${id}_mlhtree.pdf
ln -snf ${tree_path}/Tree_file/${normal}_variants.csv ${tree_path}/TreeClass/${tree_class}/${id}_variants.csv

## 驱动基因的CCF变化
ln -snf ${Images_path}/DriverEverySample/${id}_Driver.pdf ${tree_path}/TreeClass/${tree_class}/${id}_Driver.pdf
done
